#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include<string>

using namespace std;

#define NUM 5

enum class Status
{
    OK=0,
    ERROR
};

class ThreadDate
{
public:
    ThreadDate(const string& name,int id,int n)
    :_name(name),_id(id),_createTime(time(nullptr)),_n(n),_result(0),_status(Status::OK)
    {}
public:
    string _name;
    int _id;
    time_t _createTime;
    int _n;
    int _result;
    Status _status;
};

void* ThreadRun(void* arg)
{
    ThreadDate* td=static_cast<ThreadDate*>(arg);

    for(int i=0;i<td->_n;i++)
    {
        td->_result+=i;
    }

    cout<<"线程"<<td->_name<<"ID"<<td->_id<<"CreateTime"<<td->_createTime<<endl;
    pthread_exit((void*)arg);
}

int main()
{
    pthread_t pt[NUM];

    for(int i=0;i<NUM;i++)
    {
        char name[64];
        snprintf(name,sizeof(name),"thread-%d",i+1);

        ThreadDate *td=new ThreadDate(name,i,100*(10+i));
        pthread_create(pt+i,nullptr,ThreadRun,td);
        sleep(1);
    }

    void* retval=nullptr;
    for(int i=0;i<NUM;i++)
    {
        int ret=pthread_join(pt[i],&retval);
        if(ret!=0)
        cerr<<"等待线程"<<pt[i]<<"失败"<<endl;

        ThreadDate* td=static_cast<ThreadDate*> (retval);
        if(td->_status==Status::OK)
            cout<<"线程"<<pt[i]<<"运行失败"<<endl;

            cout<<"所有线程均推出"<<endl;

            return 0;
        
    }
}
